package net.cyl.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import net.cyl.emums.BizCodeEnum;
import net.cyl.exception.BizException;
import net.cyl.model.EventMessage;
import net.cyl.service.ProductOrderService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RabbitListener(queuesToDeclare = {
        @Queue("order.close.queue"),
        @Queue("order.update.queue")}
  )
public class ProductOrderMQListener {
    @Autowired
    private ProductOrderService productOrderService;
    @RabbitHandler
    public void productOrderHandler(EventMessage eventMessage, Message message, Channel channel){
        log.info("接收到订单关闭的消息:{}",eventMessage);
        try {
            productOrderService.handleProductOrderMessage(eventMessage);

        }
        catch (Exception e){
            log.error("订单关闭的消息处理失败",e);
            throw new BizException(BizCodeEnum.MQ_CONSUMER_EXCEPTION);
        }
        log.info("订单关闭的消息处理完成");
    }
}
